//
//  AppConfig.swift
//  CMB250716
//
//  Created by Assistant on 2025-01-02.
//

import Foundation
import SwiftUI

/// 应用配置常量
struct AppConfig {
    
    // MARK: - 业务配置
    
    /// Token有效期（天）
    static let tokenValidityDays = 30
    
    /// 优惠券提醒天数
    static let couponReminderDays = 3
    
    /// 会员等级积分阈值
    static let membershipThresholds = [100, 500, 1000, 5000]
    
    /// 生物识别设置延迟时间（秒）
    static let biometricSetupDelay: TimeInterval = 2.0
    
    /// 生物识别设置完成后是否自动关闭页面
    // 设置为false确保用户有足够时间看到设置成功状态并手动完成
    static let biometricSetupAutoClose: Bool = false
    
    /// 是否启用调试日志（用于诊断问题）
    static let enableDebugLogging: Bool = true
    
    /// 预计完成时间（分钟）
    static let estimatedCompletionMinutes = 15
    
    /// 会员编号起始值
    static let memberNumberStartValue = 100001
    
    /// 优惠券使用限制
    static let couponUsageLimit = 1
    
    /// 测试验证码
    static let testVerificationCode = "123456"
    
    /// 会员前缀
    static let memberPrefix = "CuMrBull"
    
    /// 订单号前缀
    static let orderPrefix = "CMB-"
    
    /// 默认语言
    static let defaultLanguage = "en"
    
    /// 最大会员数量
    static let maxMemberNumber = 999999
    
    // MARK: - 时间段配置
    
    /// 周末时间段 (最晚点餐时间6:30pm)
    static let weekendTimeSlots = [
        (start: "11:00", end: "18:30"),
        (start: "12:00", end: "14:00")
    ]
    
    /// 工作日时间段 (最晚点餐时间6:30pm)
    static let weekdayTimeSlots = [
        (start: "11:00", end: "18:30"),
        (start: "17:30", end: "18:30")
    ]
    
    // MARK: - 现金券配置
    
    /// 现金券面额
    static let cashVoucherValues = [10, 20, 30]
    
    /// 现金券数量配置
    static let cashVoucherQuantities: [String: Int] = [
        "现金券 S$10": 5,
        "现金券 S$20": 3,
        "现金券 S$30": 2,
        "超值现金礼券 S$10": 5,
        "超值现金礼券 S$20": 3,
        "超值现金礼券 S$30": 2,
        // Cash Voucher Package S$300 配置
        "现金券 1": 4,
        "现金券 2": 7,
        "现金券 3": 5,
        "Cash Voucher 1": 4,
        "Cash Voucher 2": 7,
        "Cash Voucher 3": 5,
        "Cash Voucher S$15": 4,
        "Cash Voucher S$20": 7,
        "Cash Voucher S$30": 5
    ]
    
    /// 现金券金额配置
    static let cashVoucherAmounts: [String: Double] = [
        "S$3.00": 3.0,
        "S$2.00": 2.0,
        "S$10": 10.0,
        "S$20": 20.0,
        "S$30": 30.0
    ]
    
    /// 现金券套餐配置
    struct CashVoucherPackage {
        static let packageName = "现金礼券套餐"
        static let packageEnglishName = "Cash Voucher Packages"
        static let packagePrice = 300.0
        static let packageDescription = "包含多种面额的优质现金礼券套餐"
        static let packageEnglishDescription = "Premium cash voucher package with multiple denominations"
        static let voucherValidityYears = 1
        static let voucherCodes = ["CASH_VOUCHER_1", "CASH_VOUCHER_2", "CASH_VOUCHER_3"]
        static let voucherValues = [15.0, 20.0, 30.0] // 对应三张现金券的面额
        static let minimumSpends = [20.0, 30.0, 40.0] // 对应三张现金券的最低消费要求
        
        /// 现金券数量配置（参数化配置）
        static let voucherQuantities: [Int] = [
            4, // Cash Voucher 1: 4张 S$15现金券
            7, // Cash Voucher 2: 7张 S$20现金券
            5  // Cash Voucher 3: 5张 S$30现金券
        ]
    }
    
    // MARK: - 网络配置
    
    // MARK: - 环境配置
    
    /// 应用环境类型
    enum Environment {
        case development
        case staging
        case production
    }
    
    /// 当前环境（可以通过编译配置或运行时切换）
    static let currentEnvironment: Environment = {
        #if DEBUG
        return .development
        #else
        return .production
        #endif
    }()
    
    /// 根据环境获取API基础URL
    static var apiBaseURL: String {
        switch currentEnvironment {
        case .development:
            return "http://localhost:3001"
        case .staging:
            return "https://staging-api.cumrbull.com.sg"
        case .production:
            return "https://api.cumrbull.com.sg"
        }
    }
    
    /// 示例图片URL
    static let exampleImageURL = "https://example.com/image.jpg"
    
    /// 联系邮箱
    static let contactEmail = "info@cumrbull.com.sg"
    
    // MARK: - 存储键名配置
    
    struct StorageKeys {
        static let userPrefix = "user_"
        static let ordersPrefix = "orders_"
        static let completionStatusPrefix = "completion_status_"
        static let persistentLoginPhone = "persistentLoginPhone"
        static let persistentLoginToken = "persistentLoginToken"
        static let nextOrderNumber = "nextOrderNumber"
    }
    
    // MARK: - 正则表达式配置
    
    struct RegexPatterns {
        static let email = "^[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$"
        static let phoneNumber = "^[0-9]{8}$"
        static let memberID = "^[A-Za-z0-9]+$"
    }
    
    // MARK: - 税率和折扣配置
    
    struct Financial {
        static let taxRate: Double = 0.09
        static let defaultDiscount: Double = 5.0
        static let membershipDiscountRate: Double = 0.1
    }
    
    // MARK: - 限时折扣配置
    
    /// 限时半价优惠配置
    struct LimitedTimeDiscount {
        /// 折扣比例（50%折扣）
        static let discountRate: Double = 0.5
        
        /// 工作日时间段（周一至周五 14:00-19:00）
        static let weekdayTimeSlots = [
            (start: "14:00", end: "19:00")
        ]
        
        /// 周末时间段（周六周日 11:00-20:00）
        static let weekendTimeSlots = [
            (start: "11:00", end: "20:00")
        ]
        
        /// 排除的商品类别（酒水饮料）
        static let excludedCategories: [MenuCategory] = [.drinks]
        
        /// 适用的订餐类型（仅限堂食和打包）
        static let applicableDiningTypes: [DiningType] = [.dineIn, .takeaway]
        
        /// 是否需要会员身份
        static let requiresMembership: Bool = true
        
        /// 促销卡片文案
        static let promotionTitle = (
            chinese: "限时全场半价优惠",
            english: "Limited Time 50% OFF"
        )
        
        static let promotionSubtitle = (
            chinese: "工作日14:00-19:00 | 周末全天",
            english: "Weekdays 2-7PM | Weekends All Day"
        )
        
        static let promotionDescription = (
            chinese: "所有商品半价优惠（酒水除外）\n仅限堂食与打包订单",
            english: "50% OFF All Items (Beverages Excluded)\nDine-in & Takeaway Only"
        )
        
        static let buttonText = (
            chinese: "立即查看",
            english: "View Now"
        )
    }
    
    // MARK: - UI配置
    
    struct UI {
        /// 动画持续时间
        static let animationDuration: Double = 1.5
        static let shortAnimationDuration: Double = 1.0
        
        /// 透明度值
        static let backgroundOpacity: Double = 0.9
        static let overlayOpacity: Double = 0.85
        static let mediumOpacity: Double = 0.8
        static let lightOpacity: Double = 0.7
        static let veryLightOpacity: Double = 0.6
        static let subtleOpacity: Double = 0.3
        static let faintOpacity: Double = 0.2
        static let minimalOpacity: Double = 0.1
        static let tinyOpacity: Double = 0.08
        static let microOpacity: Double = 0.05
        
        /// 线条宽度
        static let thinLineWidth: CGFloat = 1
        static let mediumLineWidth: CGFloat = 1.5
        
        /// 圆角半径
        static let smallRadius: CGFloat = 8
        static let mediumRadius: CGFloat = 12
        static let largeRadius: CGFloat = 2
        
        /// 偏移量
        static let smallOffset: CGFloat = 4
        static let mediumOffset: CGFloat = 6
        static let negativeOffset: CGFloat = -1
        
        /// 弹窗高度配置
        static let inviteViewHeightFraction: Double = 2.0/3.0 // 屏幕高度的2/3
        
        /// 邀请码卡片配置
        static let inviteCodeCardPadding: CGFloat = 12
        static let inviteCodeCardCornerRadius: CGFloat = 12
        static let inviteCodeCardSpacing: CGFloat = 8
    }
    
    // MARK: - 导航配置
    
    struct Navigation {
        /// 登录后默认跳转的Tab索引
        static let defaultTabAfterLogin = 0 // 0: Home, 1: Menu, 2: Cart, 3: Orders, 4: Me
        
        /// 使用优惠券后跳转的Tab索引
        static let tabAfterCouponUse = 1 // 跳转到Menu
        
        /// 购买现金券后跳转的Tab索引
        static let tabAfterVoucherPurchase = 2 // 跳转到Cart
    }
    
    // MARK: - 购物车配置
    
    struct Cart {
        /// 现金券套餐商品配置（使用参数化配置）
        static let cashVoucherPackageItem = (
            name: CashVoucherPackage.packageName,
            nameChinese: CashVoucherPackage.packageName,
            price: CashVoucherPackage.packagePrice,
            description: CashVoucherPackage.packageEnglishDescription,
            descriptionChinese: CashVoucherPackage.packageDescription
        )
        
        /// 现金券套餐名称（使用参数化配置）
        static let voucherPackageName = CashVoucherPackage.packageName
        
        /// 现金券套餐描述（使用参数化配置）
        static let voucherPackageDescription = CashVoucherPackage.packageDescription
        
        /// 现金券套餐价格（使用参数化配置）
        static let voucherPackagePrice = CashVoucherPackage.packagePrice
    }
    
    // MARK: - 现金券配置
    
    struct CashVoucher {
        /// 现金券代码列表（使用参数化配置）
        static let codes = CashVoucherPackage.voucherCodes
        
        /// 现金券显示策略
        enum DisplayStrategy {
            case alwaysShow // 总是显示（旧行为）
            case showAfterPurchase // 购买后才显示（新行为）
        }
        
        /// 当前显示策略
        static let displayStrategy: DisplayStrategy = .showAfterPurchase
        
        /// 未购买时的行为
        enum UnpurchasedBehavior {
            case showWithActivationCondition // 显示但标记为需要激活
            case hideCompletely // 完全隐藏
        }
        
        /// 未购买时的行为配置
        static let unpurchasedBehavior: UnpurchasedBehavior = .hideCompletely
        
        // MARK: - 布局配置
        struct Layout {
            /// Cash Voucher金额文字的左偏移量（避免被数字标识遮挡）
            static let valueTextLeftOffset: CGFloat = -20
            
            /// 数字标识的右偏移量（向左移动到与View Details按钮左边对齐）
            static let quantityBadgeRightOffset: CGFloat = -8
            
            /// 数字标识的上偏移量
            static let quantityBadgeTopOffset: CGFloat = 8
            
            /// 数字标识的圆形大小
            static let quantityBadgeSize: CGFloat = 6
            
            /// 特定现金券的徽章位置配置（参数化配置）
            static let specificVoucherBadgePositions: [String: BadgePosition] = [
                // 主要配置：匹配formattedTitle方法的实际输出
                "Cash Voucher S$10": .adjacentToTag,
                "Cash Voucher S$15": .adjacentToTag,
                "Cash Voucher S$20": .adjacentToTag,
                "Cash Voucher S$30": .adjacentToTag,
                "现金券 S$10": .adjacentToTag,
                "现金券 S$15": .adjacentToTag,
                "现金券 S$20": .adjacentToTag,
                "现金券 S$30": .adjacentToTag,
                "Value Cash Voucher S$10": .adjacentToTag,
                "Value Cash Voucher S$15": .adjacentToTag,
                "Value Cash Voucher S$20": .adjacentToTag,
                "Value Cash Voucher S$30": .adjacentToTag,
                "超值现金礼券 S$10": .adjacentToTag,
                "超值现金礼券 S$15": .adjacentToTag,
                "超值现金礼券 S$20": .adjacentToTag,
                "超值现金礼券 S$30": .adjacentToTag,
                // 备用配置：支持其他可能的标题格式
                "Cash Voucher 1": .adjacentToTag,
                "Cash Voucher 2": .adjacentToTag,
                "Cash Voucher 3": .adjacentToTag,
                "现金券 1": .adjacentToTag,
                "现金券 2": .adjacentToTag,
                "现金券 3": .adjacentToTag,
                // 关键：添加对formatValue()结果的支持（实际显示的tag值）
                // 这些是现金券实际显示的tag值，基于CashVoucherPackage.voucherValues
                "S$15": .adjacentToTag,  // Cash Voucher 1 的 formatValue() 结果
                "S$20": .adjacentToTag,  // Cash Voucher 2 的 formatValue() 结果
                "S$30": .adjacentToTag,  // Cash Voucher 3 的 formatValue() 结果
                "S$10": .adjacentToTag,
                // 添加Free标签支持（免费优惠券）
                "Free": .adjacentToTag
            ]
            
            /// 徽章位置枚举
            enum BadgePosition {
                case topRight      // 右上角（默认位置）
                case adjacentToTag // 紧邻标签
            }
        }
    }
    
    // MARK: - UI文本配置
    
    struct UIText {
        /// View All按钮文本
        static let viewAllText = (
            english: "View All",
            chinese: "查看全部"
        )
        
        /// 云朵背景配置
        static let cloudBackgroundConfig = (
            opacity: 0.7,
            cornerRadius: 12.0,
            shadowRadius: 4.0
        )
    }
    
    // MARK: - 颜色配置
    
    struct ColorConfig {
        /// RGB颜色值
        static let darkBackground = (red: 0.05, green: 0.05, blue: 0.1)
        static let orangeAccent = (red: 1.0, green: 0.5, blue: 0.0, alpha: 1.0)
        
        /// 云朵背景颜色
        static let cloudBackground = (red: 0.2, green: 0.2, blue: 0.3, alpha: 0.7)
        
        /// 渐变色配置
        static let primaryGradient = [
            Color(red: 0.05, green: 0.05, blue: 0.1),
            Color(red: 0.1, green: 0.1, blue: 0.15)
        ]
    }
}